"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:AttendanceAttendanceDetailCtrl
 * @description
 * # AttendanceAttendanceDetailCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "AttendanceAttendanceDetailCtrl",
    function (
      $location,
      DataService,
      $rootScope,
      openApi,
      $routeParams,
      Util,
      localStorageService
    ) {
      var vm = this;
      this.pkId = $routeParams.pkId || "";
      var weeks = [
        { value: 1, key: "周一" },
        { value: 2, key: "周二" },
        { value: 3, key: "周三" },
        { value: 4, key: "周四" },
        { value: 5, key: "周五" },
        { value: 6, key: "周六" },
        { value: 7, key: "周日" },
      ];
      this.clockWays = [
        { value: 0, key: "默认" },
        { value: 1, key: "只能连接wifi打卡" },
      ];
      this.clockWithSelfies = [
        { value: 0, key: "关闭" },
        { value: 1, key: "开启" },
      ];
      this.offsets = [100, 300, 500, 800, 1000, 2000];
      vm.sheetObj = {};
      // 底部关联模块类型选框添加
      var attDays = {};
      attDays.sheettitle = "选择时间";
      attDays.callback = function (days) {
        vm.param.dutyDays = days.map(function (item, index) {
          return item.value;
        });
        vm.param.cycleTime = days
          .map(function (item, index) {
            return item.key;
          })
          .toString();
      };
      vm.sheetObj.attDays = attDays;
      vm.showrelModule = function () {
        vm.sheetObj.attDays.tabList = weeks;
        vm.showSheet("attDays");
      };
      vm.showSheet = function (type) {
        var sheetObj = vm.sheetObj[type];
        vm.sheetObj.type = type;
        vm.sheetObj.tabList = sheetObj.tabList;
        vm.sheetObj.sheettitle = sheetObj.sheettitle;
        vm.sheetObj.ifshowSheet = true;
        $rootScope.$emit("showmasker");
      };
      // 提交考勤
      this.submit = function () {
        console.log(vm.param.clockObject);

        if (!vm.startTime) {
          Util.Toast("请选择上午上班时间");
          return;
        }
        if (vm.param.haveToAdTimes == 4) {
          if (!vm.endTime2) {
            Util.Toast("请选择上午下班时间");
            return;
          }
          if (!vm.startTime2) {
            Util.Toast("请选择下午上班时间");
            return;
          }
        }
        if (!vm.endTime) {
          Util.Toast("请选择下午下班时间");
          return;
        }
        _getTime();
        if (!vm.param.cycleTime) {
          Util.Toast("请选择考勤时间");
          return;
        }
        if (!checkTime()) {
          Util.Toast("下班时间要大于上班时间");
          return;
        }
        if (typeof vm.param.clockObject == "undefined") {
          Util.Toast("请选择考勤类型");
          return;
        }

        if (vm.param.clockObject == "0") {
          if (!vm.param.department || vm.param.department.length < 1) {
            Util.Toast("请选择考勤部门");
            return;
          }
        } else {
          if (!vm.param.user || vm.param.user.length < 1) {
            Util.Toast("请选择考勤人员");
            return;
          }
          vm.param.department = [-1];
        }

        if (!vm.param.address) {
          Util.Toast("请选择考勤地点");
          return;
        }
        if (
          vm.param.wifiConfigMap &&
          Object.keys(vm.param.wifiConfigMap).length > 0
        ) {
          vm.param.wifiConfig = JSON.stringify(vm.param.wifiConfigMap);
        } else {
          vm.param.wifiConfig = "";
        }
        DataService.getData(openApi.attAdd, vm.param).then(function (data) {
          Util.Toast(data, { callback: Util.goBack });
        });
      };
      function checkTime() {
        var onduty =
          parseInt(vm.param.onDutyHourAm) * 60 +
          parseInt(vm.param.onDutyMintueAm);
        var offduty =
          parseInt(vm.param.offDutyHourPm) * 60 +
          parseInt(vm.param.offDutyMintuePm);
        return offduty > onduty;
      }
      this.showusers = function () {
        this.saveparam();
        $location.path("attendance/attendance_user/" + vm.pkId);
      };

      this.chooseusers = function () {
        if (vm.param.user) {
          localStorageService.set("chooseUsrs", vm.param.user.join(","));
        }
        this.saveparam();
        // 里面包含了title
        $location.path("userdefined/choosePerson/");
      };

      this.showdeps = function () {
        this.saveparam();
        $location.path("attendance/attendance_deps/" + vm.pkId);
      };
      this.showMap = function () {
        this.saveparam();
        $location.path("attendance/edit_location");
      };
      this.confirmdel = function () {
        navigator.notification.confirm(
          "是否删除该考勤，删除后所有考勤记录将被清除",
          vm.checkConfirm,
          "删除考勤",
          ["删除", "取消"]
        );
      };
      this.checkConfirm = function () {
        var param = {};
        param.pkId = vm.pkId;
        DataService.getData(openApi.attDel, param).then(function (data) {
          Util.Toast(data, { callback: Util.goBack });
        });
      };
      this.saveparam = function () {
        _getTime();
        localStorageService.set("attDetail", vm.param);
      };
      this.choosewifi = function () {
        this.saveparam();
        $location.path("attendance/wifilist");
      };
      var init = function () {
        var options = {};
        options.pkId = vm.pkId;
        vm.param = localStorageService.get("attDetail");
        // 获取后马上清除
        localStorageService.remove("attDetail");
        var chooseUsrs = localStorageService.get("chooseUsrs");
        localStorageService.remove("chooseUsrs");
        // 获取选择页面返回的数据并且合并到参数中
        if (chooseUsrs) {
          vm.param.userNames = chooseUsrs.aduitUserNames || "";
          if (chooseUsrs.aduitUserIds) {
            vm.param.user = chooseUsrs.aduitUserIds.split(",");
          }
        }
        // 如果有缓存用缓存
        // 没有缓存则判断如果是新建则设置新对象否则请求接口数据
        if (vm.param) {
          vm.wifistr =
            vm.param.wifiConfigMap &&
            Object.keys(vm.param.wifiConfigMap)
              .map(function (item) {
                return vm.param.wifiConfigMap[item];
              })
              .toString();
          _setTime();
        } else {
          if (options.pkId) {
            DataService.getData(openApi.attGet, options).then(function (data) {
              vm.param = data;
              console.log(data);
              vm.wifistr =
                vm.param.wifiConfigMap &&
                Object.keys(vm.param.wifiConfigMap)
                  .map(function (item) {
                    return vm.param.wifiConfigMap[item];
                  })
                  .toString();
              // 初始化默认偏移
              vm.param.allowableOffset =
                vm.param.allowableOffset || vm.offsets[0];
              vm.param.haveToAdTimes = vm.param.haveToAdTimes
                ? vm.param.haveToAdTimes.toString()
                : "2";
              _setTime();
              vm.param.clockWay = vm.param.clockWay || 0;
              vm.param.clockWithSelfie = vm.param.clockWithSelfie || 0;

              if (vm.param.clockObject) {
                vm.param.clockObject = vm.param.clockObject.toString();
              }
              vm.param.userNames = vm.param.users;
            });
          } else {
            vm.param = {};
            vm.param.allowableOffset =
              vm.param.allowableOffset || vm.offsets[0];
            vm.param.clockObject = vm.param.clockObject || "0";
            vm.param.haveToAdTimes = vm.param.haveToAdTimes
              ? vm.param.haveToAdTimes.toString()
              : "2";
            vm.param.clockWay = vm.param.clockWay || 0;
            vm.param.clockWithSelfie = vm.param.clockWithSelfie || 0;
          }
        }
      };
      function _setTime() {
        vm.startTime =
          timeFormat(vm.param.onDutyHourAm, ":") +
          timeFormat(vm.param.onDutyMintueAm);
        vm.endTime =
          timeFormat(vm.param.offDutyHourPm, ":") +
          timeFormat(vm.param.offDutyMintuePm);
        vm.startTime2 =
          timeFormat(vm.param.onDutyHourPm, ":") +
          timeFormat(vm.param.onDutyMintuePm);
        vm.endTime2 =
          timeFormat(vm.param.offDutyHourAm, ":") +
          timeFormat(vm.param.offDutyMintueAm);
      }
      function timeFormat(time, format) {
        var date = "";
        if (time || time === 0) {
          if (time < 10 && time >= 0) {
            date = "0" + time;
          } else {
            date = date + time;
          }
          if (format) {
            date = date + format;
          }
        }

        return date;
      }
      function _getTime() {
        if (vm.startTime) {
          vm.param.onDutyHourAm = parseInt(vm.startTime.split(":")[0]);
          vm.param.onDutyMintueAm = parseInt(vm.startTime.split(":")[1]);
        }
        if (vm.endTime) {
          vm.param.offDutyHourPm = parseInt(vm.endTime.split(":")[0]);
          vm.param.offDutyMintuePm = parseInt(vm.endTime.split(":")[1]);
        }
        if (vm.startTime2) {
          vm.param.onDutyHourPm = parseInt(vm.startTime2.split(":")[0]);
          vm.param.onDutyMintuePm = parseInt(vm.startTime2.split(":")[1]);
        }
        if (vm.endTime2) {
          vm.param.offDutyHourAm = parseInt(vm.endTime2.split(":")[0]);
          vm.param.offDutyMintueAm = parseInt(vm.endTime2.split(":")[1]);
        }
      }
      init();
    }
  );
